#ifndef CSCNSOLID_H_
#define CSCNSOLID_H_

#include <stdio.h>
#include <iostream>
#include <vector>

#include <irrlicht.h>
#include "scntypes.h"
#include "CScnBSPTree.h"

class CScnSolid
{
private:

    int loadSurfs(std::ifstream *);
    int loadNodes(std::ifstream *);
    int loadPlanes(std::ifstream *);
    int loadVerts(std::ifstream *);
    int loadUVPos(std::ifstream *);
    int loadVertIdxs(std::ifstream *);
    int loadUVIdxs(std::ifstream *);
    int loadUnk(std::ifstream *);
    int loadCells(std::ifstream *);
    int loadPortal(scnPortal_t *, std::ifstream *);
    int loadCellData(std::ifstream * file);


    int calcUniqueTexturesNames(std::ifstream *);
    void buildBackTree();


//    const static defColor = video::SColor(128,255,255,255); //doesn't work, why?
public:
    u32 n_unk1, n_verts, n_uvpos, n_vertidxs, n_planes, n_nodes, n_surfs, n_cells, n_unk2;
    u32 offset;
    u32 length;
    int * surfsad;  //array of offset in scn file to each surface i
    int uvposad;    //offset in file to start of uvpos array

    scnSurf_t * surfs;
    scnPlane_t * planes;
	point3f	* verts;
	point2f * uvpos;
	u32 * vertidxs;
	u32 * uvidxs;
	CScnBSPTree * tree;
	scnRawCell_t * rawcells;

	//all texture names in the solid, each only once
	std::vector<std::string> textures;

    //constructor - do nothing for now
    CScnSolid ();
    ~CScnSolid();

    //load from file
    int loadSolid(std::ifstream*);

#ifdef __IRRLICHT_H_INCLUDED__
    //function only returns vertice from array
    //so it doesn't include surface specifics like color, tcoords or normal
    //irrlicht specific
    video::S3DVertex getVertice(u32 idx)
    {
        video::S3DVertex vert = video::S3DVertex(
                    verts[idx].x,verts[idx].y,verts[idx].z,        //coords
                    1,1,1,video::SColor(255,255,255,255),0,1);
        return vert;
    }
    core::array<video::S3DVertex> calcSurfVertices(u32 surfidx);
    core::array<u16> CScnSolid::calcSurfIndices(u32 surfidx);

    core::array<u32> * uvpos_caller;  //for uvpos[i], this array contains the index of uvidxs that points to it,
                                    //ie, uvpos[uvidx[uvpos_caller[i][j]]] = uvpos[i], for all j

    core::array<u32> * uvidxs_caller; //same phylosophy. Also, for scn files, each uvidx is pointed by only one surface,
                                        //so, really no point in using an array<u32>[i], could use u32[i]



#else
    vector<u16> calcSurfIndices(u32 surfidx);



#endif
};

#endif
